<?php

/**
 * 订单资金记录
 */
namespace app\order\model;

use app\system\model\SystemModel;

class OrderFundModel extends SystemModel {

    protected $infoModel = [
        'pri' => 'fund_id',
    ];

    protected function base($where) {
        return $this->table('order_fund(A)')
            ->join('member_user(B)', ['B.user_id', 'A.user_id'])
            ->field(['A.*', 'B.email(user_email)', 'B.tel(user_tel)', 'B.nickname(user_nickname)'])
            ->where((array)$where);
    }

    public function loadList($where = array(), $limit = 0, $order = '') {
        $list = $this->base($where)
            ->limit($limit)
            ->order($order)
            ->select();
        foreach ($list as $key => $vo) {
            $list[$key]['show_name'] = target('member/MemberUser')->getNickname($vo['user_nickname'], $vo['user_tel'], $vo['user_email']);
        }
        return $list;
    }

    public function countList($where = array()) {
        return $this->base($where)->count();
    }

    public function getWhereInfo($where) {
        $info = $this->base($where)->find();
        if($info) {
            $info['show_name'] = target('member/MemberUser')->getNickname($info['user_nickname'], $info['user_tel'], $info['user_email']);
        }
        return $info;
    }

    public function addLog($orderId, $userId, $money, $type = 0, $remark = '', $unit = '元') {
        if(empty($orderId) || empty($userId)) {
            $this->error = '订单号或用户名未知';
            return false;
        }
        return $this->add([
            'order_id' => $orderId,
            'time' => time(),
            'money' => $money,
            'user_id' => $userId,
            'remark' => $remark,
            'type' => $type,
            'unit' => $unit
        ]);
    }

}